package com.ljk.util;

import cn.ctyun.services.cloudtrail.ClientException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.S3ClientOptions;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.ljk.config.CtyunProperty;
import lombok.extern.slf4j.Slf4j;

import java.io.File;

@Slf4j
public class CtyunUtil {

    public static String uploadFile(CtyunProperty property) {
        String endpoint = property.getEndpoint();
        String accessKeyId = property.getAccessKeyId();
        String accessKeySecret = property.getAccessKeySecret();
        String bucketName = property.getBucketName();
        String uploadPath = property.getUploadFolder();

        File uploadFile = new File(property.getFile());
        String uploadFileName = property.getRename().isEmpty() ? uploadFile.getName() : property.getRename();
        //创建文件桶内路径
        String fileUrl = uploadPath.isEmpty() ? uploadFileName : uploadPath + "/" + uploadFileName;
        AmazonS3Client ossClient = createClient(accessKeyId, accessKeySecret);
        ossClient.setEndpoint(endpoint);
        try {
            //上传文件
            upload(ossClient, bucketName, fileUrl, uploadFile);
            return "https://" + bucketName + "." + endpoint + "/" + fileUrl;
        } catch (ClientException ce) {
            System.err.println(ce.getMessage());
            throw ce;
        }
    }

    protected static void upload(AmazonS3Client client, String bucketName, String uploadPath, File uploadFile) {
        if (uploadFile == null) {
            log.debug("===>OSS文件上传文件为空,文件路径：{}", uploadPath);
            return;
        }
        if (uploadFile.isFile()) {
            long size = uploadFile.length() / 1024 / 1024;
            if (size > 1) {
                log.debug("===>OSS文件上传开始,文件路径：{},大小:{}", uploadPath, size);
            }
            client.putObject(new PutObjectRequest(bucketName, uploadPath, uploadFile));
            log.debug("===>OSS文件上传成功,文件路径：{}", uploadPath);
            return;
        }
        File[] subFileList = uploadFile.listFiles();
        if (subFileList == null) {
            log.debug("文件夹{}没有子文件,已跳过创建", uploadFile);
            return;
        }
        for (File file : subFileList) {
            upload(client, bucketName, uploadPath + "/" + file.getName(), file);
        }
    }


    /** 创建客户端 */
    public static AmazonS3Client createClient(String accessKeyId, String accessKeySecret) {
        ClientConfiguration clientConfig = new ClientConfiguration();
        //设置连接的超时时间，单位毫秒
        clientConfig.setConnectionTimeout(30 * 1000);
        //设置 socket 超时时间，单位毫秒
        clientConfig.setSocketTimeout(30 * 1000);
        //设置 http
        clientConfig.setProtocol(Protocol.HTTPS);
        //设置 V4 签名算法中负载是否参与签名，关于签名部分请参看《OOS 开发者文档》
        S3ClientOptions options = new S3ClientOptions();
        options.setPayloadSigningEnabled(true);
        // 创建 client
        AmazonS3Client oosClient = new AmazonS3Client(new PropertiesCredentials(accessKeyId, accessKeySecret), clientConfig);
        oosClient.setS3ClientOptions(options);
        return oosClient;
    }
}
